package org.luckyjourney.service.consultants.impl;

import cn.hutool.core.util.ObjectUtil;
import org.luckyjourney.entity.consultants.TalConsultants;
import org.luckyjourney.entity.consultants.dto.PersonalDataDTO;
import org.luckyjourney.entity.consultants.dto.RetrievePWDTO;
import org.luckyjourney.entity.consultants.dto.TalConsultantsDTO;
import org.luckyjourney.entity.consultants.vo.TalConsultantsVO;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.mapper.consultants.ConsultantsMapper;
import org.luckyjourney.service.consultants.ConsultantsService;
import org.luckyjourney.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ConsultantsServiceImpl implements ConsultantsService {
    @Autowired
    ConsultantsMapper consultantsMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;//防止乱码

    @Override
    public boolean register(TalConsultants consultants) {
        String codeInSession = redisTemplate.opsForValue().get(consultants.getPhone());
        if (ObjectUtil.isEmpty(codeInSession)) {
            throw new BaseException("验证码已失效");
        }
        if (ObjectUtil.isNotEmpty(codeInSession) && codeInSession.equals(consultants.getCode())) {

            TalConsultants consultantss = consultantsMapper.selectId(consultants.getPhone());
            if (ObjectUtil.isEmpty(consultantss)) {
                consultants.setBriefIntroduction("这人很懒什么也没有留下...");
                consultants.setPassword(MD5Utils.GetMD5Code(consultants.getPassword()));
                int count = consultantsMapper.insert(consultants);
                if (count > 0) {
                    return true;
                }
            } else {
                throw new BaseException("该账户以存在");
            }
        }
        return false;
    }

    /**
     * 咨询师登录
     *
     * @param consultants
     * @return
     */
    @Override
    public TalConsultants consultantsLogin(TalConsultantsDTO consultants) {
        String phone = consultants.getPhone();
        String password1 = consultants.getPassword();
        TalConsultants consultants1 = consultantsMapper.selectId(phone);
        if (ObjectUtil.isNotEmpty(consultants1)) {
            String password = consultants1.getPassword();

            if (!MD5Utils.GetMD5Code(password1).equals(password)) {
                throw new BaseException("密码错误");
            }
        } else {
            throw new BaseException("该手机号用户不存在，请注册");
        }
        return consultants1;
    }

    @Override
    public Boolean findPassword(RetrievePWDTO retrievePWDTO) {

        String codeInSession = redisTemplate.opsForValue().get(retrievePWDTO.getPhone());
        if (ObjectUtil.isEmpty(codeInSession)) {
            throw new BaseException("验证码已失效");
        }
        if (ObjectUtil.isNotEmpty(codeInSession) && codeInSession.equals(retrievePWDTO.getCode())) {

            TalConsultants consultantss = consultantsMapper.selectId(retrievePWDTO.getPhone());
            if (ObjectUtil.isEmpty(consultantss)) {
                throw new BaseException("该手机号用户不存在，请注册");
            }
            TalConsultants talConsultants = new TalConsultants();
            talConsultants.setPassword(MD5Utils.GetMD5Code(retrievePWDTO.getNewPassword()));
            talConsultants.setPhone(retrievePWDTO.getPhone());
            int count = consultantsMapper.updatePassword(talConsultants);
            if (count > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public TalConsultants PhoneLogin(TalConsultantsDTO consultantsDTO) {
        String codeInSession = redisTemplate.opsForValue().get(consultantsDTO.getPhone());
        if (ObjectUtil.isEmpty(codeInSession)) {
            throw new BaseException("验证码已失效");
        }
        String phone = consultantsDTO.getPhone();
        if (ObjectUtil.isNotEmpty(codeInSession) && codeInSession.equals(consultantsDTO.getCode())) {

            TalConsultants consultantss = consultantsMapper.selectId(phone);
            if (ObjectUtil.isNotEmpty(consultantss)) {

                if (consultantss.getPhone().equals(phone)) {
                    return consultantss;
                } else {
                    throw new BaseException("该手机号用户不存在，请注册");
                }
            }
        }
        return null;
    }

    @Override
    public Boolean updatePassword(RetrievePWDTO retrievePWDTO) {
        String codeInSession = redisTemplate.opsForValue().get(retrievePWDTO.getPhone());
        if (ObjectUtil.isEmpty(codeInSession)) {
            throw new BaseException("验证码已失效");
        }
        if(ObjectUtil.isNotEmpty(codeInSession) && codeInSession.equals(retrievePWDTO.getCode())){
            String newPassword = retrievePWDTO.getNewPassword();
            Long id = retrievePWDTO.getId();
            TalConsultants talConsultants = consultantsMapper.selectById(id);

            String password = talConsultants.getPassword();
            if (!MD5Utils.GetMD5Code(newPassword).equals(password)) {
                TalConsultants talConsultants1 = new TalConsultants();
                talConsultants1.setPassword(MD5Utils.GetMD5Code(newPassword));
                talConsultants1.setId(id);
                int count = consultantsMapper.updateIdPW(talConsultants1);
                if (count > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean update(PersonalDataDTO personalDataDTO) {
        int count = consultantsMapper.update(personalDataDTO);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public TalConsultantsVO selectId(PersonalDataDTO personalDataDTO) {
        TalConsultantsVO talConsultantsVO=consultantsMapper.select(personalDataDTO);

        return talConsultantsVO;
    }
}
